﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AirlineProject.Models;

namespace AirlineProject.Controllers
{
    public class AccountController : Controller
    {
        public ActionResult EditProfile()
        {
            return View();
        }
        public ActionResult Login()
        {
            return View();
        }
        public ActionResult Options()
        {
            return View();
        }
        public ActionResult Book()
        {
            return View();
        }
        public ActionResult Book2(int id)
        {
            string v = string.Empty;
            if (Session["username"] == null)
            {
                v = "Login";
                ViewData["message"] = "You need to login the system.";
                return RedirectToAction("Login", "Account");
            }
            else
            {

                TypeTicketModels ttm = new TypeTicketModels();
                List<TypeTicket> tt = ttm.ListTypeTicket(id);
                TicketModels tm = new TicketModels();
                FlightModels fm = new FlightModels();
                List<Ticket> t = tm.status(id);
                Flight f = fm.seatnumflight(id);
                int kq = 0;
                for (int i = 0; i <= t.Count(); i++)
                {

                    if (i == t.Count())
                    {
                        kq = f.seat_num - i;
                        ViewData["kq"] = kq;
                    }
                }
                ViewData["infoflight"] = fm.InfoFlight(id);
                ViewData["ds"] = ttm.ListTypeTicket(id);
                v = "Book2";
            }
            
            return View(v);
        }
        public ActionResult SearchFlight(int from, int to, DateTime departure_date)
        {
            string v = string.Empty;
            List<Flight> f = new List<Flight>();
            AccountModels acm = new AccountModels();
            f = acm.SearchFlight(from,to,departure_date);
            if (f == null)
            {
                v = "SearchNotData"; 
            }
            else
            {
                Session["ds"] = acm.ListFlight(from);
                return RedirectToAction("Book", "Home");
            }
            return View(v);
        }
        public ActionResult LoginUser(string user_name, string password)
        {
            string v = string.Empty;
            AccountModels acm = new AccountModels();
            if (acm.Login(user_name, password))
            {
                Session["username"] = acm.Information(user_name);
                return RedirectToAction("Index", "Home");
            }
            else
            {
                v = "Login";
                ViewData["message"] = "Login failed";
            }
            return View(v);
        }
        public ActionResult NewUser(string user_name, string password, string first_name, string last_name, string address, string phone_number, string email, int gender, string DOB)
        {
            AccountModels acm = new AccountModels();
            User u = new Models.User();
           DateTime date = DateTime.Parse("01/01/1997");
            string v = string.Empty;
                if ((Convert.ToDateTime(DOB.Trim()) > date))
                {
                    ViewData["message"] = "you must be over 16 years old.";
                    v = "ProfileRegistration";
                }
                else
                {
                    if (acm.AddUser(user_name, password, first_name, last_name, address, phone_number, email, gender, Convert.ToDateTime(DOB), 1))
                    {
                        v = "Login";
                    }
                    else
                    {
                        v = "ProfileRegistration";
                        ViewData["message"] = "User registration failed.";
                    }
                }                    
            return View(v);
        }
        public ActionResult ProfileRegistration()
        {
            return View();
        }
        public ActionResult Search(int id)
        {
            List<Destination> list = new List<Destination>();
            AccountModels acm = new AccountModels();
            list = acm.ListCitySearch(id);
            string result = string.Empty;
            foreach (Destination o in list)
            {
                result += "<option value='"+o.place_parentid+"'>"+o.place_name+"</option>";
            }
            Response.Write(result);
            return null;
        }
    }
}
